package cn.jbit.stream.fastjson;

import cn.jbit.stream.pojo.User;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.List;

public class TestListSerial {

    @Test
    public  void  testList(){

        List<User> users = new ArrayList<>();

        User user=new User();
        user.setId(1);
        user.setName("zaicheng");
        user.setPassword("123.com");
        User user1=new User();
        user1.setId(2);
        user1.setName("张三");
        user1.setPassword("234");
        users.add(user);
        users.add(user1);
        /**
         * SerializerFeature.PrettyFormat:进行时间的序列化之后的格式化
         * SerializerFeature.WriteMapNullValue：将序列化后的未被赋值的属性赋值为null
         */
        String s = JSONArray.toJSONString(users, SerializerFeature.PrettyFormat,SerializerFeature.WriteMapNullValue);
        System.out.println(s);

        //反序列化
       // List list = JSONArray.parseObject(s, List.class);
        //new TypeReference<List<User>>:构建反序列化对应的返回类型
        List<User> list = JSONArray.parseObject(s, new TypeReference<List<User>>() {
        });
        System.out.println(list.get(1).getName());

        JSONArray jsonArray = JSONArray.parseArray(s);
        System.out.println(jsonArray);


    }
}
